/**
 *
 */
package co.fitstart.payment.alipay.pay.handler.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;

import co.fitstart.payment.alipay.AlipayURL;
import co.fitstart.payment.alipay.ReturnCode;
import co.fitstart.payment.alipay.pay.AlipayCommonParameter;
import co.fitstart.payment.alipay.pay.AlipayWapPayParameter;
import co.fitstart.payment.alipay.pay.handler.AlipayPayHandler;

/**
 * @author lute
 *
 */
@Component("alipayPayHandler")
public class AlipayPayHandlerImpl implements AlipayPayHandler {

    private static final Logger logger = LoggerFactory.getLogger(AlipayPayHandlerImpl.class);

    /* (non-Javadoc)
     * @see co.fitstart.payment.alipay.pay.handler.AlipayPayHandler#requestWapPay(co.fitstart.payment.alipay.pay.AlipayCommonParameter, co.fitstart.payment.alipay.pay.AlipayWapPayParameter)
     */
    public String requestWapPay(AlipayCommonParameter commonParameter, AlipayWapPayParameter wapPayParameter) {
        String appid = commonParameter.getAppid();
        String privateKey = commonParameter.getPrivateKey();
        String publicKey = commonParameter.getPublickKey();
        String format = commonParameter.getFormat();
        String charset = commonParameter.getCharset();
        String signType = commonParameter.getSignType();

        AlipayClient client = new DefaultAlipayClient(AlipayURL.WAP_PAY, appid, privateKey, format, charset, publicKey, signType);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setReturnUrl(wapPayParameter.getReturnURL());
        request.setNotifyUrl(wapPayParameter.getNotifyURL());
        // request.setProdCode(wapPayParameter.getProductCode());
        request.setApiVersion(commonParameter.getVersion());

        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setSubject(wapPayParameter.getSubject());
        model.setOutTradeNo(wapPayParameter.getOutTradeNo());
        model.setTotalAmount(wapPayParameter.getTotalAmountAsYuan());
        model.setProductCode(wapPayParameter.getProductCode());
        if (StringUtils.isNotBlank(wapPayParameter.getBody())) {
            model.setBody(wapPayParameter.getBody());
        }
        if (StringUtils.isNoneBlank(wapPayParameter.getAuthToken())) {
            model.setAuthToken(wapPayParameter.getAuthToken());
        }
        if (StringUtils.isNotBlank(wapPayParameter.getTimeoutExpress())) {
            model.setTimeoutExpress(wapPayParameter.getTimeoutExpress());
        }
        if (StringUtils.isNotBlank(wapPayParameter.getSellerId())) {
            model.setSellerId(wapPayParameter.getSellerId());
        }
        request.setBizModel(model);

        try {
            return client.pageExecute(request).getBody();
        } catch (AlipayApiException e) {
            logger.error("AlipayWapPayHandlerImpl#requestWapPay - Requesting Alipay wap pay encounters Alipay API exception: {}", e);
        }
        return null;
    }

    /* (non-Javadoc)
     * @see co.fitstart.payment.alipay.pay.handler.AlipayPayHandler#queryOrder(co.fitstart.payment.alipay.pay.AlipayCommonParameter, java.lang.String, java.lang.String)
     */
    public AlipayTradeQueryResponse queryOrder(AlipayCommonParameter commonParameter, String requestNo, String tradeNo) {
        String appid = commonParameter.getAppid();
        String privateKey = commonParameter.getPrivateKey();
        String publicKey = commonParameter.getPublickKey();
        String format = commonParameter.getFormat();
        String charset = commonParameter.getCharset();
        String signType = commonParameter.getSignType();

        AlipayClient client = new DefaultAlipayClient(AlipayURL.QUERY_ORDER, appid, privateKey, format, charset, publicKey, signType);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setApiVersion(commonParameter.getVersion());

        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(requestNo);
        model.setTradeNo(tradeNo);
        request.setBizModel(model);

        try {
            AlipayTradeQueryResponse response = client.execute(request);
            String code = response.getCode();
            if (!ReturnCode.SUCCESS.equals(code)) {
                logger.error("AlipayPayHandlerImpl#queryOrder - Querying order encounters error: {}", response.getMsg());
            } else {
                return response;
            }
        } catch (AlipayApiException e) {
            logger.error("AlipayPayHandlerImpl#queryOrder - Querying order encounters exception: {}", e);
        }
        return null;
    }

}
